package com.example.farmeri.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmeri.R;
import com.example.farmeri.common.OnExtraClickListener;
import com.example.farmeri.http.API;
import com.example.farmeri.pojo.Roles;
import com.example.farmeri.tools.ResponseUtils;
import com.example.farmeri.ui.strategy.ViewStrategyActivity;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * Create by weiqishan on 2020/4/13.
 */
public class AccessPolicyAdapter extends BaseRecycleViewAdapter<Roles.RoleListBean> {

    Context context;
    Integer screeWidth;
    Integer layout;
    OnExtraClickListener onExtraClickListener;
    Roles roles;

    public AccessPolicyAdapter(Context context, int layout, int screeWidth) {
        super(context,layout);
        this.context = context;
        this.layout = layout;
        this.screeWidth = screeWidth;

        fetch(1,999);
    }

    private void fetch(int page, int limit) {
        getRetrofit().create(API.class)
                .getRoles(String.valueOf(page),String.valueOf(limit))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils<Roles>>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull ResponseUtils<Roles> roleResponseUtils) {
                        if (roleResponseUtils.getCode() == 200){
                            roles = roleResponseUtils.getData();
                            roles.getRoleList().forEach(element -> {dataList.add(element);});
//                            AccessPolicyAdapter.this.notifyDataSetChanged();
                            notifyItemInserted(dataList.size());
                        }else {
                            showMsg(roleResponseUtils.getMessage());
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }



    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = inflater.inflate(layout,viewGroup,false);
        return new MyViewHolder(itemView);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
        Roles.RoleListBean bean = dataList.get(i);
        myViewHolder.name.setText(bean.getName());
        myViewHolder.type.setText(bean.getIdentity());
        //String updateDate = DataUtils.getStringDate(new Date());
        myViewHolder.time.setText(""+bean.getUpdateDate());
        //设置显示布局占满一行
        myViewHolder.layout_show.setMinWidth(screeWidth);
        //点击显示布局勾选checkbox
        myViewHolder.layout_show.setOnClickListener(v -> {
            if (myViewHolder.container.getScrollX()>0){
                myViewHolder.container.smoothScrollTo(0,0);
                return;
            }
            myViewHolder.checkBox.setChecked(!myViewHolder.checkBox.isChecked());
        });
        //滑动效果
        myViewHolder.container.setOnTouchListener((v, event) -> {
            int action = event.getActionMasked();
            switch(action){
                case MotionEvent.ACTION_UP:
                    int scrollX = myViewHolder.container.getScrollX();
                    int hideLayoutWidth = myViewHolder.layout_hide.getWidth();
                    //滑动距离超过隐藏布局的一半
                    if (scrollX > hideLayoutWidth/2){
                        myViewHolder.container.smoothScrollTo(hideLayoutWidth,0);
                    }else {
                        myViewHolder.container.smoothScrollTo(0,0);
                    }
                    return true;
                default:
                    break;
            }
            return false;
        });
        //点击checkbox
        myViewHolder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                onExtraClickListener.onChecked(myViewHolder.getAdapterPosition());
            }else{
                onExtraClickListener.onUnChecked(myViewHolder.getAdapterPosition());
            }
        });
        //查看策略详情
        myViewHolder.detail.setOnClickListener(v -> {
            Intent intent = new Intent(context, ViewStrategyActivity.class);
            intent.putExtra("id",bean.getId());
            context.startActivity(intent);
        });
        //点击删除
        myViewHolder.del.setOnClickListener(v -> {
            //onExtraClickListener.onDelClick(v,MyViewHolder.getAdapterPosition());
        });
    }

    public void setOnExtraClickListener(OnExtraClickListener onExtraClickListener) {
        this.onExtraClickListener = onExtraClickListener;
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {

        HorizontalScrollView container;
        ConstraintLayout layout_show;
        LinearLayout layout_hide;
        TextView name; //策略名称
        TextView type; //策略类型
        TextView time; //修改时间
        TextView del; //删除
        TextView detail; //详情
        CheckBox checkBox; //选择框

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            container = itemView.findViewById(R.id.item_access_policy_container);
            layout_show = itemView.findViewById(R.id.item_access_policy_layout_show);
            layout_hide = itemView.findViewById(R.id.item_access_policy_layout_hide);
            name = itemView.findViewById(R.id.item_access_policy_name);
            type = itemView.findViewById(R.id.item_access_policy_type);
            time = itemView.findViewById(R.id.item_access_policy_time);
            detail = itemView.findViewById(R.id.item_access_policy_detail);
            del = itemView.findViewById(R.id.item_access_policy_del);
            checkBox = itemView.findViewById(R.id.item_access_policy_check);
        }
    }
}
